#include "chooselevelscene.h"
#include <QMenuBar>
#include <QPainter>
#include <QTimer>
#include <QLabel>
#include <QtMultimedia/QSound>
#include "mypushbutton.h"
#include "playscene.h"

ChooseLevelScene::ChooseLevelScene(QWidget *parent) : QMainWindow(parent)
{
    init();
    createLevelButton();
    setConnect();
}

void ChooseLevelScene::init(){
    this->setFixedSize(320,588);
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    this->setWindowTitle("选择关卡");
    QMenuBar* bar = this->menuBar();
    //this->setMenuBar(bar);
    QMenu* startMenu = bar->addMenu("开始");
    m_QuitAction = startMenu->addAction("退出");
    m_pPlayScene = nullptr;
    m_CloseBtn = new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png",this);
    m_CloseBtn->move(this->width()-m_CloseBtn->width(),this->height()-m_CloseBtn->height());
}

void ChooseLevelScene::setConnect(){
    connect(m_QuitAction,&QAction::triggered,this,&ChooseLevelScene::onAcitionQuitClicked);
    connect(m_CloseBtn,&QPushButton::clicked,this,&ChooseLevelScene::onCloseBtnClicked);
    for(int i = 0;i < vecLevelButton.size();i++){
        auto levelSelectButton = vecLevelButton[i];
        connect(levelSelectButton,&MyPushButton::clicked,[=](){
            QSound* chooseSound = new QSound(":/res/TapButtonSound.wav",this);
            chooseSound->play();
            emit LevelSelected(i+1);
        });
    }
    connect(this,&ChooseLevelScene::LevelSelected,this,&ChooseLevelScene::OnLevelSelected);
}

void ChooseLevelScene::onAcitionQuitClicked(){
    this->close();
}

void ChooseLevelScene::paintEvent(QPaintEvent*){
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/OtherSceneBg.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);

    //加载标题
    pix.load(":/res/Title.png");
    painter.drawPixmap((this->width()-pix.width())*0.5,30,pix.width(),pix.height(),pix);
}

void ChooseLevelScene::onCloseBtnClicked(){
    QSound* backSound=new QSound(":/res/BackButtonSound.wav",this);
    backSound->play();
    QTimer::singleShot(500,this,&ChooseLevelScene::startReturnToMain);
}

void ChooseLevelScene::startReturnToMain(){
    this->hide();
    emit chooseSceneBack();
}

void ChooseLevelScene::createLevelButton(){
    for(int i = 0;i < 20;i++){
        MyPushButton* menuBtn = new MyPushButton(":/res/LevelIcon.png");
        menuBtn->setParent(this);
        menuBtn->move(25 + (i%4) * 70,130 + (i/4)*70);
        QLabel* label = new QLabel;
        label->setParent(this);
        label->setFixedSize(menuBtn->width(),menuBtn->height());
        label->setText(QString::number(i+1));
        label->setAlignment(Qt::AlignHCenter|Qt::AlignVCenter);
        label->move(25 + (i%4)*70,130 +(i/4)*70);
        label->setAttribute(Qt::WA_TransparentForMouseEvents,true);
        vecLevelButton.push_back(menuBtn);
        //connect(menuBtn,&MyPushButton::clicked,
    }
}

void ChooseLevelScene::OnLevelSelected(int level){
    if(m_pPlayScene == nullptr){
        this->hide();
        m_pPlayScene = new PlayScene(level);
        connect(m_pPlayScene,&PlayScene::playSceneBack,this,&ChooseLevelScene::BackToChoose);
        m_pPlayScene->show();
    }else{
        disconnect(m_pPlayScene,&PlayScene::playSceneBack,this,&ChooseLevelScene::BackToChoose);
        delete m_pPlayScene;
        m_pPlayScene = nullptr;
    }
}

void ChooseLevelScene::BackToChoose(){
    this->show();
    delete m_pPlayScene;
    m_pPlayScene = nullptr;
}
